package operator;

import book.Book;
import book.BookList;

import java.util.Scanner;

public class DelOperator implements Operator{

    @Override
    public void work(BookList bookList) {
        System.out.println("------删除图书------");
        System.out.println("请输入要删除图书的书名：");
        Scanner sc = new Scanner(System.in);
        String s = sc.nextLine();

//        int p = bookList.getUsebook();
//        for (int i = p; i < bookList.getBooks().length - 1; i++) {
//            Book tmp = bookList.getBooks(i + 1 );
//            bookList.setBooks(tmp, p);
//        }
//        bookList.setBooks(null, p);
//        System.out.println("删除成功");

        int i = 0;
        int p = bookList.getUsebook();
        for ( i = 0; i < p; i++) {
            if(s.equals(bookList.getBooks(i).getName())) {
                break;
            }
        }
        if(i == p) {
            System.out.println("书架中没有该书");
            return;
        }
            for (int j = i; j < bookList.getBooks().length - 1; j++) {
            Book tmp = bookList.getBooks(j + 1 );
            bookList.setBooks(tmp, j);
            }
            bookList.setBooks(null, p);
            bookList.setUsebook(p - 1);
            System.out.println("删除成功");


    }
}
